//
//  ViewController.swift
//  HexColorConvert
//
//  Created by Admin on 2024/11/1.
//

import Cocoa

class ViewController: NSViewController {

    @IBOutlet weak var hexColorTextField: NSTextField!
    
    @IBOutlet weak var output1: NSTextField!
    @IBOutlet weak var output2: NSTextField!
    
    @IBOutlet weak var logLabel: NSTextField!
    
    @IBOutlet weak var resultColor: NSBox!
    
    override func viewDidLoad() {
        super.viewDidLoad()
    }
    
    @IBAction func convertAction(_ sender: NSButton) {
        hexColorToUIColor(hex: hexColorTextField.stringValue)
    }
    
    
    func hexColorToUIColor(hex: String) {
        
        logLabel.stringValue = ""
        
        var hexSanitized = hex.trimmingCharacters(in: .whitespacesAndNewlines)
        if hexSanitized.hasPrefix("#") {
            hexSanitized.remove(at: hexSanitized.startIndex)
        }
        
        // 检查长度
        guard hexSanitized.count == 6 else {
            logLabel.stringValue = "16进制颜色内容错误"
            return
        }
        
        var rgb: UInt64 = 0
        Scanner(string: hexSanitized).scanHexInt64(&rgb)
        
        let r = (rgb >> 16) & 0xFF
        let g = (rgb >> 8) & 0xFF
        let b = rgb & 0xFF
        
        let red = CGFloat(r) / 255.0
        let green = CGFloat(g) / 255.0
        let blue = CGFloat(b) / 255.0
        
        let color1 = "UIColor(red: \(r)/255.0, green: \(g)/255.0, blue: \(b)/255.0, alpha: 1.0)"
        let color2 = "UIColor(red: \(String(format: "%.2f", red)), green: \(String(format: "%.2f", green)), blue: \(String(format: "%.2f", blue)), alpha: 1.0)"
        
        output1.stringValue = color1
        output2.stringValue = color2
        
        logLabel.stringValue = "转换完毕"
        resultColor.fillColor = NSColor(red: red, green: green, blue: blue, alpha: 1)
    }
    
    @IBAction func copyOutput1(_ sender: NSButtonCell) {
        
        if output1.stringValue.count == 0 {
            logLabel.stringValue = "内容为空"
            return
        }
        
        let textToCopy = output1.stringValue
        let pasteboard = NSPasteboard.general
        pasteboard.clearContents() // 清空剪切板
        pasteboard.setString(textToCopy, forType: .string) // 设置字符串到剪切板
        
        logLabel.stringValue = "已将文本复制到剪切板"
    }
    
    @IBAction func copyOutput2(_ sender: NSButtonCell) {
        
        if output2.stringValue.count == 0 {
            logLabel.stringValue = "内容为空"
            return
        }
        
        let textToCopy = output2.stringValue
        let pasteboard = NSPasteboard.general
        pasteboard.clearContents() // 清空剪切板
        pasteboard.setString(textToCopy, forType: .string) // 设置字符串到剪切板
        
        logLabel.stringValue = "已将文本复制到剪切板"
    }
    
    @IBAction func clearInput(_ sender: Any) {
        hexColorTextField.stringValue = ""
        logLabel.stringValue = ""
    }
    
    
    override var representedObject: Any? {
        didSet {
        // Update the view, if already loaded.
        }
    }


}

